package com.wanhe.shoppingcart;

import com.wanhe.base.model.base.SallOutDish;
import com.wanhe.base.model.entity.BaseResultEntity;
import com.wanhe.base.model.holder.SallOutHolder;
import com.wanhe.base.net.ServerApi;
import com.wanhe.base.runner.Runner;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.bean.SellOutResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by XieZaiWei on 2017/5/9.
 */

public class SellOutPresenter {

	private ServerApi api;
	private Runner runner;

	public SellOutPresenter(){
		api = new ServerApi();
		runner = new Runner();
	}

	/**
	 * 获取估清数据
	 *
	 * @author 陈德基
	 * created at 2016/8/5 10:56
	 */
	public void getSellOutData() {
		JSONObject object = new JSONObject();
		try {
			object.put("paramJsonStr", "{\"op\":\"PollDishWarnList\"}");

			runner.execute(api.getSellOutData(), new Consumer<SellOutResult>() {

				@Override
				public void accept(@NonNull SellOutResult sellOutResult) throws Exception {
					if (ObjectUtil.checkObject(sellOutResult)) {
						if (sellOutResult.resultType == BaseResultEntity.SUCCESS) {
							List<SallOutDish> sallOutDishs = sellOutResult.warnDishes;
							if (ObjectUtil.checkObjectList(sallOutDishs)) {
								SallOutHolder holder = SallOutHolder.getInstance();
								holder.clear();
								for (SallOutDish sallOutDish : sallOutDishs) {
									holder.put(sallOutDish.getDishID(), sallOutDish);
								}
							} else {
//								Logger.i(TAG, "该店没有估清数据");
							}
						} else {
//							Logger.e(TAG, resultEntity.resultMsg);
						}
					} else {
//						Logger.e(TAG, "没有估清数据");
					}

				}
			});
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
